package com.mediacloud.app.newsmodule.fragment.navlive

import androidx.lifecycle.ViewModel
import com.chinamcloud.project.yunfu.net.MutableLiveDataX
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.ArticleListData
import com.mediacloud.app.model.news.kt.TypeX
import com.mediacloud.app.newsmodule.model.NavigateListReciver
import com.mediacloud.app.reslib.util.DataInvokeUtil
import com.mediacloud.app.user.utils.LogUtil
import com.mediacloud.app.user.utils.RxUtils
import com.mediaclound.appfactnet.utils.RxTransformerUtils
import org.json.JSONObject

const val TYPE_GET_CONTENTID=1
const val TYPE_GET_VIDEO_LIVE=2
const val TYPE_GET_VDIDEO_VOD=3
const val TYPE_GET_AUDIO_LIVE=4

class TxqcLiveNavVM: ViewModel()
{
    var contentIdObserver=MutableLiveDataX<JSONObject>()
    var videoLiveContentList=MutableLiveDataX<ArticleListData>()
    var channelChildListObs=MutableLiveDataX<ArticleListData?>()
    var radioLiveListObs=MutableLiveDataX<ArticleListData?>()
    var channelCatalogItemObs=MutableLiveDataX<NavigateListReciver?>()
    private var pageSize:Int=60
    fun getNavContentList(navId:String,pageNumber:Int,invokeType:Int)
    {
        var result= DataInvokeUtil.getContentList(navId,pageNumber,pageSize,null)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxTransformerUtils.rsj2j())
        when(invokeType)
        {
            TYPE_GET_CONTENTID->
            {
                result.subscribe(
                        {
                            LogUtil.logw("AppFac","$it")
                            contentIdObserver.postValue(it)
                        }
                        ,
                        {
                            LogUtil.logw("AppFac","$it")
                        })
            }
            TYPE_GET_VIDEO_LIVE->
            {
                result.map {
                    LogUtil.logw("AppFac","$it")
                    var articleList=ArticleListData()
                    articleList.readFromJson(it)
                    var resultArticleListData=articleList.articleList?.filter {item:ArticleItem ->
                        if(item.getType()>= TypeX.HAMMER)
                            return@filter false
                        return@filter true
                    }

                    resultArticleListData?.apply {
                        articleList.articleList.clear()
                        articleList.articleList.addAll(this)
                    }
                    return@map articleList
                }.subscribe(
                        {
                            videoLiveContentList.postValue(it)
                        }
                        ,
                        {
                            LogUtil.logw("AppFac","$it")
                            videoLiveContentList.postValue(null)
                        }
                )
            }
            TYPE_GET_VDIDEO_VOD->
            {
                result.map {
                    LogUtil.logw("AppFac","$it")
                    var articleList=ArticleListData()
                    articleList.readFromJson(it)
                    return@map articleList
                }.subscribe(
                        {
                            channelChildListObs.postValue(it)
                        }
                        ,
                        {
                            LogUtil.logw("AppFac","$it")
                            channelChildListObs.postValue(null)
                        }
                )
            }
            TYPE_GET_AUDIO_LIVE->
            {
                result.map {
                    LogUtil.logw("AppFac","$it")
                    var articleList=ArticleListData()
                    articleList.readFromJson(it)
                    return@map articleList
                }.subscribe(
                        {
                            radioLiveListObs.postValue(it)
                        }
                        ,
                        {
                            LogUtil.logw("AppFac","$it")
                            radioLiveListObs.postValue(null)
                        }
                )
            }
        }
    }

    /**
     * 取左边那个频道列表  调的是children接口
     */
    fun getChannelCatalogItem(catalogId:String)
    {
        DataInvokeUtil.getZiMeiTiApi().getSecondNav(catalogId,1).map{
            var reciver=NavigateListReciver()
            reciver.readFromJson(it)
            return@map reciver
        }.compose(RxUtils.schedulersTransformer())
                .subscribe(
                        {
                            channelCatalogItemObs.postValue(it)
                        }
                        ,
                        {
                            channelCatalogItemObs.postValue(null)
                        }
                )
    }
}